/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package indexWriters;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import map.file.FileMap;

public class NameIndexWriter {

	private static NameIndexWriter me = null;
	private Map<String, Set<String>> i= new HashMap<String, Set<String>>();
	
	public static NameIndexWriter getInstance() {
		if(me == null)
			me = new NameIndexWriter();  
		return me;
	}

	public void index(String s, List<String> nombres) {
		for(String nombre:nombres){
			if(i.get(nombre)==null)
				i.put(nombre, new HashSet<String>());
			i.get(nombre).add(s);
		}
	}
	
	public void write(DataOutputStream os) throws IOException{
		for(String soundex:i.keySet()){
			FileMap.writeString(os, soundex);
			os.writeShort(i.get(soundex).size());
			for(String file:i.get(soundex))
				FileMap.writeString(os, file);
		}
	}

}
